const { Sequelize } = require('sequelize');

// 数据库配置
const sequelize = new Sequelize('lingxi_system', 'root', '888mei525', {
  host: '127.0.0.1',
  dialect: 'mysql',
  logging: false
});

async function checkRoleUsers() {
  try {
    // 测试数据库连接
    await sequelize.authenticate();
    console.log('数据库连接成功');
    
    // 获取角色ID为2的角色信息
    const [roleResults] = await sequelize.query('SELECT id, name FROM roles WHERE id = 2');
    if (roleResults.length > 0) {
      console.log('角色信息:', roleResults[0]);
      
      // 检查是否有用户使用该角色
      const [userResults] = await sequelize.query('SELECT id, name, role FROM users WHERE role = ? OR role = (SELECT name FROM roles WHERE id = 2)', {
        replacements: [roleResults[0].name]
      });
      
      console.log('使用该角色的用户数量:', userResults.length);
      if (userResults.length > 0) {
        console.log('使用该角色的用户列表:');
        userResults.forEach(user => {
          console.log(`- ID: ${user.id}, 姓名: ${user.name}, 角色: ${user.role}`);
        });
      }
    } else {
      console.log('未找到ID为2的角色');
    }
    
    await sequelize.close();
  } catch (error) {
    console.error('检查失败:', error.message);
  }
}

checkRoleUsers();